package com.yyq.adms.usermanage.bpo.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yyq.adms.sysmanage.pojo.SysModules;
import com.yyq.adms.usermanage.bpo.SysRPMBpo;
import com.yyq.adms.usermanage.dao.SysRPMDao;
import com.yyq.adms.usermanage.pojo.SysPerm;
import com.yyq.adms.usermanage.pojo.SysRPM;
import com.yyq.sos.service.BaseBpoImpl;

@Transactional
@Service("sysRPMBpo")
public class SysRPMBpoImpl extends BaseBpoImpl implements SysRPMBpo {

	private SysRPMDao dao;
	
	@Resource(name="sysRPMDao")
	public void setDao(SysRPMDao dao) {
		this.dao = dao;
		this.setBaseDao(dao);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.yyq.adms.usermanage.bpo.SysRPMBpo#saveRPM(java.lang.Long, java.lang.Long, java.lang.String[])
	 */

	public boolean saveRPM(Long roleId, Long moduleId, String[] mps) {
		long num = getBrotherRPMNum(roleId,moduleId);
		List<SysRPM> rpms = new ArrayList<SysRPM>();
		
		getParentRPM(roleId,moduleId,rpms);
		
		List<SysRPM> delRPMs = new ArrayList<SysRPM>();
		boolean delCmd = false;
		if(num==0){
			delCmd = true;
			for(int i=0;i<rpms.size();i++){
				SysRPM rpm = rpms.get(i);
				if(delCmd){
					delRPMs.add(rpm);
				}
				num = getBrotherRPMNum(roleId,rpm.getModuleId());
				if(num==0){
					delCmd = true;
				}else{
					delCmd = false;
				}
			}
		}
		if(dao.execHql(getDelRPMSql(delRPMs,moduleId,roleId))){
			if(mps != null){
				List perms = dao.search("from "+SysPerm.class.getName()+" where permName='READ'");
				SysPerm read = (SysPerm)perms.get(0);
				List<SysRPM> list = new ArrayList<SysRPM>();
				List<SysModules> ms = new ArrayList<SysModules>();
				getParentModule(roleId,moduleId,ms);
				for(int i=0;i<ms.size();i++){
					SysRPM rpm = new SysRPM();
					rpm.setModuleId(ms.get(i).getId());
					rpm.setPermId(read.getId());
					rpm.setRoleId(roleId);
					list.add(rpm);
				}
				for(int i=0;i<mps.length;i++){
					SysRPM rpm = new SysRPM();
					rpm.setModuleId(moduleId);
					rpm.setPermId(new Long(mps[i]));
					rpm.setRoleId(roleId);
					list.add(rpm);
				}
				if(dao.add(list.toArray())!=null)
					return true;
				else
					return false;
			}else{
				return true;
			}
		}else{
			return false;
		}
	}

	
	private String getDelRPMSql(List<SysRPM> rpms,long moduleId, Long roleId){
		String hql = "delete "+SysRPM.class.getName()+" where (moduleId="+moduleId;
		long[] ids = new long[rpms.size()];
		for(int i=0;i<rpms.size();i++){
			hql += " or moduleId="+rpms.get(i).getModuleId();
		}
		hql +=") and roleId="+roleId+")";
System.out.println("delete sql===>"+hql);
		return hql;
	}
	
	private void getParentRPM(long roleId,long moduleId,List<SysRPM> list){
		String hql = "select obj from SysRPM obj where obj.roleId="+roleId+" and obj.moduleId=" +
				"(select parentId from SysModules where id="+moduleId+")";
		List<SysRPM> temp = dao.search(hql);
		if(temp != null && temp.size()>0){
			SysRPM rpm = (SysRPM)temp.get(0);
			list.addAll(temp);
			getParentRPM(roleId,rpm.getModuleId(),list);
		}
	}
	
	private void getParentModule(long roleId,long moduleId,List<SysModules> ms){
		String hql = "select p from SysModules c,SysModules p where c.parentId=p.id " +
				"and c.id="+moduleId+" and p.id not in(select moduleId from SysRPM where roleId="+roleId+")" ;
		List<SysModules> p = dao.search(hql);
		if(p != null && p.size()>0){
			SysModules m = p.get(0);
			ms.add(m);
			getParentModule(roleId,m.getId(),ms);
		}
	}
	
	
	public long getBrotherRPMNum(long roleId,long moduleId){
		String sql = "select count(*) from SysRPM obj,SysModules m where m.id=obj.moduleId and obj.roleId="+roleId+" and m.id<>"+moduleId
		+" and m.parentId=(select m2.parentId from SysModules m2 where m2.id="+moduleId+")";
		long count = dao.count(sql);
		return count;
	}

}
